﻿/*
 * Created by SharpDevelop.
 * User: christophe
 * Date: 7/09/2011
 * Time: 21:42
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Linq;

namespace MetaVideoEditorEvolved.UI
{
	/// <summary>
	/// Description of EntityExtensions.
	/// </summary>
	public static class EntityExtensions
	{
		public static Entity.PosterMediaItem ToPosterEntity(this mveEngine.Poster poster)
		{
			var posterToReturn = new Entity.PosterMediaItem()
			{
				Height = poster.Height,
				Id = poster.Id,
				Image = poster.Image,
				Thumb = poster.Thumb,
				Width = poster.Width,
				Checked = poster.Checked
			};
			
			posterToReturn.IsDirty = false;
			return posterToReturn;
		}
		
		public static Entity.ActorMediaItem ToActorEntity(this mveEngine.Actor actor)
		{
			return new Entity.ActorMediaItem()
			{
				ImagePath = actor.ImagePath,
				Name = actor.Name,
				Role = actor.Role
			};
		}
		
		public static Entity.CrewMemberMediaItem ToCrewMember(this mveEngine.CrewMember crewMember)
		{
			return new Entity.CrewMemberMediaItem()
			{
				Activity = crewMember.Activity,
				Name = crewMember.Name
			};
		}
		
		public static void CopyToMediaUiEntity(this mveEngine.Item item, Entity.MediaItemBaseEntity itemToFillIn)
		{
			try {
				Entity.VideoMediaElement video = null;
				
				switch (item.Type) {
					case mveEngine.Entity.Episode:
						var episode = itemToFillIn as Entity.EpisodeMediaItem;
						episode.EpisodeNumber = item.EpisodeNumber;
						video = episode;
						break;

					case mveEngine.Entity.Season:
						var season = itemToFillIn as Entity.SeasonMediaItem;
						season.SeasonNumber = item.SeasonNumber;
						video = season;
						break;
						
					case mveEngine.Entity.Series:
						var serie = itemToFillIn as Entity.SerieMediaItem;
						serie.Banners.Clear();
						
						if (item.BannersPaths!=null) {
							foreach (var banner in item.BannersPaths) {
								serie.Banners.Add(banner.ToPosterEntity());
							}
						}
						
						if(item.PrimaryBanner!=null)
						{
							serie.PrimaryBanner = item.PrimaryBanner.ToPosterEntity();
						}
						else
						{
							serie.PrimaryBanner = null;
						}
						
						serie.PrimaryProviderId = item.PrimaryProviderSerieId;
						
						video = serie;
						break;
						
					case mveEngine.Entity.Movie:
						video = itemToFillIn as Entity.VideoMediaElement;
						break;
						
					case mveEngine.Entity.Folder:
						break;
						
					default:
						throw new Exception("Invalid value for Entity");
				}
				
				if (video!=null) {
					video.IsInError = item.IsInError;
					video.Name = item.Title;
					video.Overview = item.Overview;
					video.DataState = item.DataState;
					video.DateAdded = item.DateAdded;
					video.HasChanged = item.HasChanged;
					video.MPAARating = item.MPAARating;
					video.OriginalTitle = item.OriginalTitle;
					video.Rating = item.Rating;
					video.RunningTime = item.RunningTime;
					video.SortTitle = item.SortTitle;
					video.Title = item.Title;
					video.Year = item.Year;
					video.Path = item.Path;
					video.MediaType = item.Mediatype;
					video.MetadataLocation = item.MetadataLocation;
					
					if (item.PrimaryBackdrop!=null) {
						video.PrimaryBackdrop = item.PrimaryBackdrop.ToPosterEntity();
					}
					else
					{
						video.PrimaryBackdrop = null;
					}
					
					video.Posters.Clear();
					if (item.ImagesPaths!=null) {
						foreach (var p in item.ImagesPaths) {
							video.Posters.Add(p.ToPosterEntity());
						}
					}
					
					video.Backdrops.Clear();
					if (item.BackdropImagePaths!=null) {
						foreach (var b in item.BackdropImagePaths) {
							
							video.Backdrops.Add(b.ToPosterEntity());
						}
					}
					
					video.Actors.Clear();
					if (item.Actors!=null) {
						foreach (var element in item.Actors) {
							video.Actors.Add(element.ToActorEntity());
						}
					}
					
					video.CrewMembers.Clear();
					if (item.Crew!=null) {
						foreach (var crew in item.Crew) {
							video.CrewMembers.Add(crew.ToCrewMember());
						}
					}
					
					video.Genres.Clear();
					if (item.Genres!=null) {
						foreach (var genre in item.Genres) {
							video.Genres.Add(genre);
						}
					}
					
					video.Trailers.Clear();
					if (item.TrailerFiles!=null) {
						foreach (var trailer in item.TrailerFiles) {
							video.Trailers.Add(
								new Entity.TrailerMediaItem()
								{
									FilePath = trailer
								});
						}
					}
					
					if (item.PrimaryImage!=null) {
						video.PrimaryPoster = item.PrimaryImage.ToPosterEntity();
					}
					else
					{
						video.PrimaryPoster = null;
					}
				}
			}
			catch (Exception) {
				
				throw;
			}
		}
	}
}
